package com.github.codemperor.core.message.dingtalk.robot.base;

import com.alibaba.fastjson.JSONObject;
import com.github.codemperor.core.logger.DefaultLogger;
import org.springframework.web.client.RestTemplate;

import java.util.List;

public abstract class BaseDingRobot {
    private DefaultLogger log = DefaultLogger.logger(this.getClass());
    public static final String dingtalkRobotUrl = "";

    private RestTemplate restTemplate = new RestTemplate();

    /**
     * @param title      文本标题，可以不填写
     * @param rowContent 每一行的发送内容，支持markdown语法，参考https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
     * @param isAtAll    是否at所有人，默认否
     * @param mobs       需要at的人的手机
     */
    protected abstract Object packageRequest(String title, List<String> rowContent, boolean isAtAll, String... mobs);

    /**
     * 获取最终发送的消息体
     *
     * @return
     */
    protected abstract Object getMsgRequest();

    /**
     * 同步发送
     */
    public Object send() {
        try {
            Object msgObject = getMsgRequest();
            log.info("[sendDingtalkRobotMarkdownMsg] request: {}", JSONObject.toJSONString(msgObject));
            Object result = restTemplate.postForEntity(dingtalkRobotUrl, msgObject, Object.class);
            log.info("[sendDingtalkRobotMarkdownMsg] response: {}", JSONObject.toJSONString(result));
            return result;
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    }

    /**
     * 异步发送，使用队列机制
     */
    public void asyncSend() {
        Object msgObject = getMsgRequest();
        MsgQueue.setQueue(msgObject);
        MsgQueue.customer();
    }
}
